package com.jvxb.manage.configuration.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jvxb.common.utils.DateUtil;
import com.jvxb.manage.livable.entity.DbMonitorData;
import com.jvxb.manage.livable.service.DbMonitorDataService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 监控数据采集任务
 *
 * @author jvxb
 * @description: 生产上应由每台机器上的 platform-agent程序采集数据并上报到 kafka后消费。此处通过定时任务模拟采集监控数据。
 * @since 2022-02-15
 */
@Log4j2
@Component
@ConditionalOnExpression("'${env}'.matches('pre|prd')")
public class MonitorDataCollectJob {

    @Autowired
    DbMonitorDataService monitorDataService;

    /**
     * 每60s采集一次集群数据。
     */
    @Scheduled(cron = "*/60 * * * * *")
    public void collectDbMonitorDataJob() {
        //采集mysql监控数据
        monitorDataService.collectMysqlMonitorData();
        //采集redis监控数据
        monitorDataService.collectRedisMonitorData();
    }

    /**
     * 每5min采集一次机器数据。
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void collectMonitorDataJob() {
        monitorDataService.collectAgentMonitorData();
    }

    /**
     * 每天凌晨2点清理一周前的数据
     */
    @Scheduled(cron = "0 0 2 */1 * ?")
    public void deteleMonitorDataJob() {
        //清理7天前的数据
        log.info("开始清空10日前的 monitor data 数据...");
        QueryWrapper deleteWrapper = new QueryWrapper();
        deleteWrapper.lt(DbMonitorData.CREATE_TIME, DateUtil.getAfterDayWeek("-10"));
        monitorDataService.remove(deleteWrapper);
    }


}
